

package yf.mall.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import yf.mall.vo.down.LotteryRecordDown;

import java.math.BigDecimal;
import java.util.List;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import yf.mall.dao.LotteryRecordDao;
import yf.mall.entity.LotteryRecordEntity;
import yf.mall.vo.LotteryRecordVo;
import yf.mall.service.LotteryRecordService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Aug 20 11:07:03 CST 2020
 */
@Service
public class LotteryRecordServiceImpl extends BaseServiceImpl<LotteryRecordEntity> implements LotteryRecordService {

    private LotteryRecordDao dao;


    @Autowired
    public void setDao(LotteryRecordDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<LotteryRecordVo> findVo(LotteryRecordVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public LotteryRecordVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(LotteryRecordVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(LotteryRecordVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<LotteryRecordVo> list) {
        try {
            List<LotteryRecordDown> datas = list.stream().map(item -> new LotteryRecordDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), LotteryRecordDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/
    @Override
    public BigDecimal totalActIdAndGoodId(Long  actId, Long goodsId, Long userId) {
        Integer totalAct = this.dao.totalActIdAndGoodsId(actId, null);
        Integer totalGoods = this.dao.totalActIdAndGoodsId(actId, goodsId);
        BigDecimal divide = BigDecimal.ZERO;
        if (totalGoods > 0 && totalAct > 0) {
            divide = new BigDecimal(totalGoods).divide(new BigDecimal(totalAct),8,BigDecimal.ROUND_HALF_UP);
        }
        return divide;
    }

    @Override
    public List<LotteryRecordEntity> findByUserId(Long userId, int page, int limit) {
        PageHelper.startPage(page, limit).setOrderBy(" time desc");
        return this.dao.findByUserId(userId);
    }


}
